package me.jiatao.hilt.entrypoint

import android.app.Application
import android.content.Context
import android.util.Log
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import javax.inject.Inject

class WorkService @Inject constructor() {
    fun hello() {
        Log.e("WorkService", "------ WorkService hello  ------")
    }
}

@EntryPoint
@InstallIn(SingletonComponent::class)
interface InitializerEntryPoint {

    fun injectWorkService(): WorkService

    companion object {
        fun resolve(context: Context): InitializerEntryPoint {
            val appContext = context.applicationContext ?: throw IllegalStateException()

            return EntryPointAccessors.fromApplication(
                appContext,
                InitializerEntryPoint::class.java
            )
        }
    }
}


class WorkServiceProvider(private val application: Application) {
    private val workService: WorkService by lazy {
        InitializerEntryPoint.resolve(application).injectWorkService()
    }

    fun doWork() {
        workService.hello()
    }
}

